package com.kantboot.util.core.param;

import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.io.Serializable;

/**
 * 用来请求分页的参数的封装类
 *
 * @author 方某方
 */
@Data
@Accessors(chain = true)
public class PageParam<T> implements Serializable {

    private Integer pageSize;

    private Integer pageNumber;

    private T data;

    /**
     * 排序方式
     */
    private String orderBy;

    /**
     * 排序字段
     */
    private String sort;

    public Pageable getPageable() {
        if(sort==null){
            return Pageable.ofSize(pageSize).withPage(pageNumber-1);
        }

        return new Pageable() {
            @Override
            public int getPageNumber() {
                return pageNumber-1;
            }

            @Override
            public int getPageSize() {
                return pageSize;
            }

            @Override
            public long getOffset() {
                return 0;
            }

            @Override
            public Sort getSort() {
                if(sort==null){
                    return null;
                } else if(orderBy==null){
                    return Sort.by(Sort.Order.asc(sort));
                }else if(orderBy.equalsIgnoreCase("DESC")){
                    return Sort.by(Sort.Order.desc(sort));
                }
                return Sort.by(Sort.Order.asc(sort));
            }

            @Override
            public Pageable next() {
                return null;
            }

            @Override
            public Pageable previousOrFirst() {
                return null;
            }

            @Override
            public Pageable first() {
                return null;
            }

            @Override
            public Pageable withPage(int pageNumber) {
                return null;
            }

            @Override
            public boolean hasPrevious() {
                return false;
            }
        };
    }

}
